package com.example.filter;

import cn.dev33.satoken.servlet.util.SaTokenContextJakartaServletUtil;
import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * SaTokenContext 上下文初始化过滤器 (基于 Servlet)
 *
 * @author click33
 * @since 1.42.0
 */
@Slf4j
public class SaTokenContextFilterForServlet implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		try {
			log.info("----------------------AAAAAAAAAAAAAAAAAAAAAAA_-----------------------");
			SaTokenContextJakartaServletUtil.setContext((HttpServletRequest) request, (HttpServletResponse) response);
//			if (!StpUtil.isLogin()) {
//				// 未登录，返回 401 状态码
//				log.info("----------------------");
//				return;
//			}
			chain.doFilter(request, response);
		} finally {
			SaTokenContextJakartaServletUtil.clearContext();
		}
	}

}